package com.fang.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.fang.entity.Orders;
import com.fang.entity.OrdersItem;
import com.fang.entity.PCD;
import com.fang.utils.JDBCUtils;
import com.fang.utils.Status;

public class OrdersDao implements Status{

	public List<PCD> queryPCDbypid(String pid) {
		QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
		String sql = "select * from province_city_district where pid=?";
		try {
			return qr.query(sql, new BeanListHandler<PCD>(PCD.class), pid);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("三级联动查询"+e);
		}
	}

	public void addOrders(Orders orders) {
		QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
		String sql = "insert into orders values(?,?,?,?,?,?)";
		try {
			qr.update(JDBCUtils.getConnection(),sql,orders.getId(),orders.getUid(),
					orders.getTotalprice(),orders.getAddress(),orders.getStatus(),orders.getCreatetime());
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("...");
		}
	}

	public void addOrdersItem(List<OrdersItem> olist) {
		/*
		 * 二维数组参数：
		 * 1维：执行的次数
		 * 2维：?的个数
		 */
		QueryRunner qr = new QueryRunner();
		String sql = "insert into ordersitem values(?,?,?)";
		try {
			Object[][] pa = new Object[olist.size()][3];
			for (int i = 0; i < pa.length; i++) {
				pa[i][0]=olist.get(i).getOid();
				pa[i][1]=olist.get(i).getGid();
				pa[i][2]=olist.get(i).getBuynum();
			}
			qr.batch(JDBCUtils.getConnection(),sql, pa);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException("清除购物车出错"+e);
		}
	}

	public List<Orders> querybyuid(int uid) {
		QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
		String sql = "select * from orders where uid=? ";
		try {
			return qr.query(sql, new BeanListHandler<Orders>(Orders.class), uid);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException("查询我的订单"+e);
		}
	}

	public void cansel(int uid, String id) {
		QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
		String sql = "update orders set status = ? where id = ? and uid = ?";
		try {
			qr.update(sql,STATUS_ORDERS.canceled,id,uid);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException("取消订单失败"+e);
		}
		
		
	}

	public Orders query(String id, int uid) {
		QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
		String sql = "select * from orders where id=? and uid=?";
		try {
			return qr.query(sql, new BeanHandler<Orders>(Orders.class), id,uid);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException("查询失败"+e);
		}
	}

	public List<OrdersItem> queryitem(String id) {
		
		QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
		String sql = "select * from ordersitem where oid=?";
		try {
			return qr.query(sql, new BeanListHandler<OrdersItem>(OrdersItem.class), id);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException("查询详情"+e);
		}
	}

	public void newadd(int uid, String id) {
		QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
		String sql = "update orders set status = ? where id = ? and uid = ?";
		try {
			qr.update(sql,STATUS_ORDERS.nopay,id,uid);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException("..."+e);
		}
		
	}

	public void paysucess(int uid, String id) {
		QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
		String sql = "update orders set status = ? where id = ? and uid = ?";
		try {
			qr.update(sql,STATUS_ORDERS.payed,id,uid);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException("..."+e);
		}
	}

}
